package com.kingyea.app.market.appauthorization.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.kingyea.app.market.appauthorization.bean.AppAuthorization;
import com.kingyea.app.market.appauthorization.bean.AppAuthorizationPo;
import com.kingyea.app.market.appauthorization.dao.AppAuthorizationMapper;
import com.kingyea.app.market.appauthorization.service.AppAuthorizationService;
import com.kingyea.app.market.utils.ExceptionConditions;
import com.kingyea.app.market.utils.StateCode;
import com.kingyea.app.market.utils.page.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hx
 * @version v1.0.0
 * @description AppAuthorization 实现类
 * @date Created in 2018-05-02 11:34:13
 */
@Service
@Transactional
public class AppAuthorizationServiceImpl implements AppAuthorizationService {

    private static final Logger LOGGER = LoggerFactory.getLogger(AppAuthorizationServiceImpl.class);

    @Resource
    private AppAuthorizationMapper appAuthorizationMapper;

    /**
     * 通过ID查找对象
     *
     * @param id
     * @return
     */
    @Override
    public AppAuthorization getById(Integer id) throws Exception {
        return this.appAuthorizationMapper.getById(id);
    }

    /**
     * 列表
     *
     * @param
     * @return
     */
    @Override
    public PageInfo<AppAuthorization> list(AppAuthorization appAuthorization) throws Exception {
        PageInfo.startPage(JSON.parseObject(JSON.toJSONString(appAuthorization), Maps.<String, Object>newHashMap().getClass()));
        return PageInfo.getPageInfo(appAuthorizationMapper.list(appAuthorization));
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteById(Integer id) throws Exception {
        return appAuthorizationMapper.deleteById(id) > 0;
    }

    /**
     * 保存
     *
     * @param appAuthorization
     * @return
     */
    @Override
    public boolean save(AppAuthorization appAuthorization) throws Exception {
        return appAuthorizationMapper.save(appAuthorization) > 0;
    }

    /**
     * 更新
     *
     * @param appAuthorization
     * @return
     */
    @Override
    public boolean update(AppAuthorization appAuthorization) throws Exception {
        return appAuthorizationMapper.update(appAuthorization) > 0;
    }

    @Override
    public boolean createAll(AppAuthorizationPo po) throws Exception {
        ExceptionConditions.checkStringNotEmpty(po.getAppCode(), StateCode.NO_ARGUMENT_ERROR);
        ExceptionConditions.checkNotNull(po.getMarketCode(), StateCode.NO_ARGUMENT_ERROR);
        ExceptionConditions.checkNotNull(po.getType(), StateCode.NO_ARGUMENT_ERROR);
        ExceptionConditions.checkArrayNotEmpty(po.getAuthorizationArr(), StateCode.NO_ARGUMENT_ERROR);

        List<AppAuthorization> appAuthorizationList = po.getAuthorizationArr().stream().map(str -> {
            AppAuthorization appAuthorization = new AppAuthorization();
            appAuthorization.setAppCode(po.getAppCode());
            appAuthorization.setMarketCode(po.getMarketCode());
            switch (po.getType()) {
                case 1:
                    appAuthorization.setOrgSerialNo(str);
                    break;
                case 2:
                    appAuthorization.setRoleId(Integer.valueOf(str));
                    break;
                case 3:
                    appAuthorization.setPolice(Integer.valueOf(str));
                    break;
                case 4:
                    appAuthorization.setUserId(str);
                    break;
            }
            //appAuthorization.setUserId(SecurityUtil.g);
            appAuthorization.setAuthorizationUserId("-1");
            appAuthorization.setType(po.getType());
            appAuthorization.setCreateDate(new Date());
            return appAuthorization;
        }).collect(Collectors.toList());

        return appAuthorizationMapper.batchSave(appAuthorizationList) == appAuthorizationList.size();
    }

    @Override
    public List<AppAuthorization> getbyUser(AppAuthorizationPo appAuthorization) throws Exception {
        return appAuthorizationMapper.getbyUser(appAuthorization);
    }

}
